package controls
{
	import flash.geom.Rectangle;
	
	import mx.collections.IList;
	import mx.containers.Canvas;
	import mx.controls.Button;
	import mx.core.IFactory;
	import mx.core.UIComponent;

	public class FlowBox extends UIComponent
	{
		private var _prevButton:Button;
		private var _nextButton:Button;
		private var _itemsContainer:Canvas;
		
		private var _paddingBottom:Number=5;
		private var _paddingLeft:Number=5;
		private var _paddingRight:Number=5;
		private var _paddingTop:Number=5;
		
		private var _gapBtwnBtnsAndContainer:Number = 2;
		
		private var _hGap:Number = 5;
		private var _vGap:Number = 5;
		
		private var _direction:String = FlowBoxDirection.HORIZONTAL;
		
		private var _itemRenderer:IFactory;
		private var _dataProvider:IList;
		
		public function FlowBox()
		{
			super();
		}
		override protected function createChildren():void
		{
			//when this instance is added to a parent then this method is called
			super.createChildren();
			
			_prevButton = new Button();
			_prevButton.toolTip = "Previus";
			
			_nextButton = new Button();
			_nextButton.toolTip = "Next";
			
			_itemsContainer = new Canvas();
			_itemsContainer.setStyle("verticalAlign","center");
			
			addChild(_itemsContainer);
			addChild(_prevButton);
			addChild(_nextButton);
		}
		override protected function commitProperties():void
		{
			super.commitProperties();
		}
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.commitProperties();
			
			if(unscaledHeight == 0 || unscaledWidth == 0)
				return;
			if(_direction == FlowBoxDirection.HORIZONTAL)
			{
				var btnWidth:Number = 20;
				var sx:Number = 0;
				var sy:Number = 0;
				
				var actualArea:Rectangle = new Rectangle(sx+_paddingLeft,sy+_paddingTop,unscaledWidth-(_paddingLeft+_paddingRight),unscaledHeight-(_paddingBottom+_paddingTop));
				var contentArea:Rectangle = new Rectangle(actualArea.x+btnWidth+_gapBtwnBtnsAndContainer,actualArea.y,actualArea.width-(2*btnWidth+2*_gapBtwnBtnsAndContainer),actualArea.height);
				
				_prevButton.width=btnWidth;
				_prevButton.height=actualArea.height;
				_nextButton.width = btnWidth;
				_nextButton.height = actualArea.height;
				_itemsContainer.width = contentArea.width;
				_itemsContainer.height = contentArea.height;
				
				_prevButton.x=actualArea.x;
				_prevButton.y = actualArea.y;
				
				_itemsContainer.x=contentArea.x;
				_itemsContainer.y = contentArea.y;
				
				_nextButton.x=contentArea.x+contentArea.width+_gapBtwnBtnsAndContainer;
				_nextButton.y=actualArea.y;
				
				//Draw Outer Border
				graphics.clear();
				graphics.lineStyle(1);
				graphics.drawRect(sx,sy,unscaledWidth,unscaledHeight);
				graphics.drawRect(contentArea.x,contentArea.y,contentArea.width,contentArea.height);
				//Draw Container border
			}
			
		}
		override protected function measure():void
		{
			//If the default width and height is not set in MXML tag then this method will be called
			//If this instance is not created by MXML and created by actionscript, then this method will be called after addchild call
			super.measure();
			
			measuredWidth =200;
			measuredHeight = 100;
			measuredMinHeight = 100;
			measuredMinWidth = 100;
		}
	}
}